package com.anyisn.anyflickr.shell.model
{
	import com.adobe.webapis.flickr.User;
	import com.adobe.webapis.flickr.events.FlickrResultEvent;
	import com.memamsa.airdb.DB;
	import com.memamsa.airdb.Modeler;
	
	import mx.events.CloseEvent;
	
	import com.anyisn.anyflickr.shell.ShellFacade;
	import com.anyisn.anyflickr.shell.model.sqlite.Person;
	import com.anyisn.anyflickr.shell.model.sqlite.Tag;
	import com.anyisn.anyflickr.shell.view.PeopleWindowMediator;
	import com.anyisn.anyflickr.shell.view.PersonTWMediator;
	import com.anyisn.anyflickr.shell.view.components.popup.PersonTW;
	import com.anyisn.flickr.as3.patterns.proxy.FlickrServiceProxy;

	public class PeopleProxy extends FlickrServiceProxy
	{
		public static const NAME:String = 'PeopleProxy';
		
		public var person:User;
		private var _psnTags:Array;
		private var _filterTagId:Object;
		
		public function PeopleProxy(data:Object=null)
		{
			super(NAME, data);
			
			DB.initDB('main.db');
			_psnTags = new Array();
		}
		
		public function getPersonList():void {
			_filterTagId = null;
			var psn:Person = new Person();
			var people:Array = psn.findAll(new Object());
			sendNotification(ShellFacade.PERSON_GET_LIST_SUCCESS, people);
		}
		
		public function getPersonByTag(tagid:uint):void {
			_filterTagId = tagid;
			var tag:Object = Modeler.findery(Tag, {id: tagid});
			if(tag) {
				var people:Array = tag.people.list();
				sendNotification(ShellFacade.PERSON_GET_LIST_SUCCESS, people);	
			}
		}
		
		public function pushTag(id:uint):void {
			var psn:Object = Modeler.findery(Person, {userid: person.nsid});
			if(!psn) {
				psn = new Person();
				psn.create({username: person.username, userid: person.nsid});
			}
			psn.tags.push(id);
			refreshTags(psn);
		}
		
		public function removeTag(id:uint):void {
			var psn:Object = Modeler.findery(Person, {userid: person.nsid});
			psn.tags.remove([id]);
			refreshTags(psn);
		}
		
		public function addTag(tagname:String):void {
			var tag:Tag = new Tag();
			tag.create({name: tagname});
			pushTag(tag.id);
			getTagsList(PeopleWindowMediator.NAME);
		}
		
		public function deleteTag(tagid:uint):void {
			var tag:Object = Modeler.findery(Tag, {id: tagid});
			if(tag) {
				var people:Array = tag.people.list();
				var ids:Array = new Array();
				for(var i:uint=0;i<people.length;i++) {
					ids.push(people[i].id);
				}
				tag.people.remove(ids);
				tag.remove();
				
				getTagsList("refreshAll");
			}
		}
		
		private function refreshTags(psn:Object):void {
			_psnTags = psn.tags.list();
			getTagsList(PersonTWMediator.NAME);
			if(_filterTagId) {
				getPersonByTag(_filterTagId as uint);
			} else {
				getPersonList();
			}
		}
		
		public function getTagsList(noteName:String):void {
			var tag:Tag = new Tag();
			var tags:Array = tag.findAll(new Object());
			for(var i:uint=0;i<tags.length;i++) {
				tags[i].selected = false;
				for(var j:uint=0;j<_psnTags.length;j++) {
					if(tags[i].id == _psnTags[j].id) {
						tags[i].selected = true;
					}
				}
			}
			switch(noteName) {
				case PeopleWindowMediator.NAME:
					sendNotification(ShellFacade.TAGS_GET_LIST_FOR_PEOPLE_WINDOW_SUCCESS, tags);
					break;
				case PersonTWMediator.NAME:
					sendNotification(ShellFacade.TAGS_GET_LIST_FOR_PERSON_TW_SUCCESS, tags);
					break;
				default:
					sendNotification(ShellFacade.TAGS_GET_LIST_FOR_PEOPLE_WINDOW_SUCCESS, tags);
					sendNotification(ShellFacade.TAGS_GET_LIST_FOR_PERSON_TW_SUCCESS, tags);
			}
		}
		
		public function deletePerson(nsid:String):void {
			var psn:Object = Modeler.findery(Person, {userid: nsid});
			if(psn) {
				var tags:Array = psn.tags.list();
				var ids:Array = new Array();
				for(var i:uint=0;i<tags.length;i++) {
					ids.push(tags[i].id);
				}
				psn.tags.remove(ids);
				psn.remove();
				if(_filterTagId) {
					getPersonByTag(_filterTagId as uint);
				} else {
					getPersonList();
				}
				if(facade.hasMediator(PersonTWMediator.NAME)) {
					var med:PersonTWMediator = facade.retrieveMediator(PersonTWMediator.NAME) as PersonTWMediator;
					if(person.nsid == nsid) {
						med.close();
					}
				}
			}
		}
		
		/*public function addPerson(tags:Array):void {
			var psn:Object = Modeler.findery(Person, {userid: person.nsid});
			if(!psn) {
				psn = new Person();
				psn.create({username: person.username, userid: person.nsid});
				for(var i:uint=0;i<tags.length;i++) {
					psn.tags.push(tags[i].id);
				}
			} else {
				for(var i:uint=0;i<tags.length;i++) {
					for(var j:uint=0;j<_psnTags.length;j++) {
						if(tags[i].id == _psnTags[j].id) {
							if(tags[i].selected == false) {
//								Associator.remove(obj:*) donot support Integer, but Array
								psn.tags.remove([tags[i].id]);
							}
							break;
						}
						if(j==(_psnTags.length-1) && tags[i].selected) {
							psn.tags.push(tags[i].id);
						}
					}
				}
			}
			_psnTags = psn.tags.list();
		}*/
		
		// PEOPLE_GET_INFO
		public function getPeopleInfo(nsid:String):void {
			var psn:Object = Modeler.findery(Person, {userid: nsid});
			if(psn) {
				_psnTags = psn.tags.list();
			} else {
				_psnTags = new Array();;
			}
			service.addEventListener(FlickrResultEvent.PEOPLE_GET_INFO, onPeopleGetInfo);
			service.people.getInfo(nsid);
		}
		
		private function onPeopleGetInfo(event:FlickrResultEvent):void {
			service.removeEventListener(FlickrResultEvent.PEOPLE_GET_INFO, onPeopleGetInfo);
			if(event.success) {
				person = event.data.user;
				sendNotification(ShellFacade.PEOPLE_GET_INFO_SUCCESS);
			} else {
				onFlickrError(event);
				sendNotification(ShellFacade.PEOPLE_GET_INFO_FAILED);
			}
		}
		
		// Check whether a person already exists. If not, add him.
		/*public function getPerson(user:User):XMLList {
			var person:XMLList = new XMLList(<Person nsid={user.nsid} username={user.username}></Person>);
			var cursor:IViewCursor = people.createCursor();
			var found:Boolean = cursor.findAny(person);
			if(!found) {
				people.disableAutoUpdate();
				if(people.length == 0) {
					xml..Person = person;
				} else {
					people.addItem(person);
				}
				people.enableAutoUpdate();
				people.refresh();
			} else {
				person = new XMLList(cursor.current);
			}
			return person as XMLList;
		}*/
		
		/*public function removePerson(nsid:String):void {
			for (var i:uint=0; i < people.length; i++) {
				if (people[i].@nsid == nsid) {
					people.removeItemAt(i);
					break;
				}
			}
		}*/
		
		// GETTER
		/*public function get people():XMLListCollection {
			var people:XMLListCollection = new XMLListCollection(xml..Person);
			var personSort:Sort = new Sort();
			personSort.fields = [new SortField('@username')];
			people.sort = personSort;
			people.refresh();
			
			return people;
		}
		
		public function get xml():XMLList {
			return data as XMLList;
		}*/
	}
}